﻿Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.Windows.Threading
Imports System.Data.SqlClient
Imports System.Data



Public Class CTModelMsgNotificacion
    Implements INotifyPropertyChanged, IDisposable



  Private _LstMensajes     As ObservableCollection(Of Mensaje)
  Private _UIDispacher     As Dispatcher
  Private _SQLNotificacion As CFLNotificacionDB
  Private _CountMsg  As Integer


  Public numHilo As Integer = 0


  Delegate Sub MyDelegate(dt As DataTable)



Public Sub new(pUIDispatcher As Dispatcher)

 _UIDispacher = pUIDispatcher
 _SQLNotificacion = New CFLNotificacionDB()

 AddHandler _SQLNotificacion.NuevoMsg ,AddressOf Notificar_NuevoMensaje
 Dim dt As DataTable = _SQLNotificacion.RegistrarDependencia()
 Me.CargarMensaje(dt)

End Sub


Public Sub new(pUIDispatcher As Dispatcher, ByVal  pnumHilo As Integer)
 hilo +=1

 numHilo = pnumHilo

 _UIDispacher = pUIDispatcher
 _SQLNotificacion = New CFLNotificacionDB()

 AddHandler _SQLNotificacion.NuevoMsg ,AddressOf Notificar_NuevoMensaje
 Dim dt As DataTable = _SQLNotificacion.RegistrarDependencia()
 Me.CargarMensaje(dt)

End Sub


Public Sub new()
 
_CountMsg = -1 
 _SQLNotificacion = New CFLNotificacionDB()

 AddHandler _SQLNotificacion.NuevoMsg ,AddressOf Notificar_NuevoMensaje
 Dim dt As DataTable = _SQLNotificacion.RegistrarDependencia()
 Me.CargarMensaje(dt)

End Sub





Public  Property CountMsg() As Integer
    Get
        Return _CountMsg
    End Get
   
   Set(value As Integer)      
      _CountMsg = value
      NotificarCambio("CountMsg")
   End Set

End Property

Public ReadOnly Property LstMensajes() As ObservableCollection(Of Mensaje)
    Get

        If _LstMensajes Is Nothing Then
           _LstMensajes = New ObservableCollection(Of Mensaje)
        End If

        Return _LstMensajes
    End Get

End Property

Public Property UIDispacher() As Dispatcher
    Get
        Return _UIDispacher
    End Get
    Set(ByVal value As Dispatcher)
        _UIDispacher = value
    End Set
End Property

Public Property SQLNotificacion() As CFLNotificacionDB
    Get
        Return _SQLNotificacion 
    End Get
    Set(ByVal value As CFLNotificacionDB)
        _SQLNotificacion = value
    End Set
End Property

Public Sub Notificar_NuevoMensaje(sender As Object, e As SqlNotificationEventArgs)  
' El controlador que está registrado para procesar las notificaciones realiza las 
' acciones necesarias para cambiar del subproceso de notificación, un subproceso del grupo de subprocesos, 
' al subproceso de interfaz de usuario y, a continuación, se vuelve a registrar para recibir notificaciones.

  Me.CargarMensaje(Me._SQLNotificacion.RegistrarDependencia)       
End Sub


private Sub CargarMensaje(dt As DataTable) 
Dim myArray(0) As Object
myArray(0) = dt

If not Me.UIDispacher is Nothing then 
    Me.UIDispacher.BeginInvoke(New MyDelegate(AddressOf HiloEjec),myArray) 
End If
End Sub

public Sub HiloEjec(dt As DataTable)
 If Not dt is Nothing then        

     If Me.CountMsg <> dt.Rows.Count then 
       Me.CountMsg = dt.Rows.Count 
     End If
    
 End If



End Sub


#Region "Notificación de la clase"

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotificarCambio(ByVal propname As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propname))
    End Sub

#End Region

#Region "IDisposable Support"
    Private disposedValue As Boolean' To detect redundant calls

    ' IDisposable
    Protected     Overridable     Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class


Public Class Mensaje

 Private _Id       As Integer
 Private _Msg      As String
 Private _FecHora  As DateTime




 Public Sub new()

 End Sub

Public Sub New(ByVal pId As Integer, ByVal pMsg As String)
  _Id = pId
  _Msg = pMsg
End Sub




Public Property Id() As Integer
    Get
        Return _Id
    End Get
    Set(ByVal value As Integer)
        _Id = value
    End Set
End Property

Public Property Msg() As String
    Get
        Return _Msg
    End Get
    Set(ByVal value As String)
        _Msg = value
    End Set
End Property

Public Property FecHora() As DateTime
    Get
        Return _FecHora 
    End Get
    Set(ByVal value As DateTime)
        _FecHora = value
    End Set
End Property
   
End Class
